<template>
  <el-dialog
    :title="showTitle"
    :visible="dialogVisible"
    @close="handleCloseDialog"
  >
    <!-- @open="handleOpen" -->
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in simpleList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="handleConfirm">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
import { Message } from 'element-ui'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    depts: {
      type: Array,
      required: true
    },
    nodeDate: {
      type: Object,
      required: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      let arr
      if (this.form.id) {
        arr = this.depts.filter(item => item.pid === this.nodeDate.pid)
        // if (arr.name === value) {
        //   return callback()
        // }
        arr.forEach(item => {
          if (item.name === value) {
            return callback()
          }
        })
        // console.log(arr.name)
      } else {
        arr = this.depts.filter(item => item.pid === this.nodeDate.id)
      }
      console.log(arr)
      const isRepeat = arr.some(item => item.name === value)
      isRepeat ? callback(new Error('同级部门名称不允许重复')) : callback()
    }
    const validateCode = (rule, value, callback) => {
      if (this.form.id) {
        if (value === this.nodeDate.code) {
          return callback()
        }
      }

      const isRepeat = this.depts.some(item => item.code === value)
      isRepeat ? callback(new Error('部门编码不允许重复')) : callback()
    }
    return {
      form: {
        name: '',
        introduce: '',
        manager: '',
        code: ''
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }],
        code: [{ required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入部门描述', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: ['blur', 'change'] }],
        manager: [{ required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }
        ]
      },
      simpleList: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    async handleConfirm() {
      try {
        await this.$refs.form.validate()
        console.log(this.form.id)
        if (this.form.id) {
          console.log(this.form)
          await reqUpdateDepartments(this.form)
          Message.success('修改成功')
        } else {
          await reqAddDepartments({
            ...this.form,
            pid: this.nodeDate.id
          })
          Message.success('新增部门成功')
        }
        this.$emit('add-depts')
        this.handleCloseDialog()
      } catch (error) {
        Message.error('添加失败')
      }
    },

    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      this.simpleList = res.data
    },
    handleCloseDialog() {
      this.$emit('update:dialogVisible', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        introduce: '',
        manager: '',
        code: '',
        id: ''
      }
    },
    async getDepartDetail(id) {
      const { data } = await reqGetDepartDetail(id)
      // console.log(data)
      this.form = data
    }
  }
}
</script>

<style>

</style>
